package com.servlet.ihm;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LogoutServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		request.getSession().invalidate();

		Cookie[] cookies = request.getCookies();
		
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if ("SESSION_LOGIN_USERNAME".equals(cookie.getName())) {
					cookie.setMaxAge(0);
					cookie.setPath("/");
					cookie.setValue(null);
					response.addCookie(cookie);
				}
				if ("SESSION_LOGIN_PASSWORD".equals(cookie.getName())) {
					cookie.setMaxAge(0);
					cookie.setPath("/");
					cookie.setValue(null);
					response.addCookie(cookie);
				}
			}
		}

		RequestDispatcher dispatch = request.getRequestDispatcher("index.jsp");
		dispatch.forward(request, response);
	}
}
